package com.deplume.ssm.controller.admin.home;

import com.deplume.ssm.pojo.admin.DeliveryActivity;
import com.deplume.ssm.pojo.admin.Job;
import com.deplume.ssm.pojo.common.Result;
import com.deplume.ssm.pojo.common.ResultDeliveryActivityList;
import com.deplume.ssm.pojo.common.ResultJobList;
import com.deplume.ssm.pojo.home.JobTmp;
import com.deplume.ssm.service.inface.admin.home.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by blake on 2016/8/17.
 */
@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    ICompanyService companyService;

    @RequestMapping("insertJobById")
    public String insertJobById(HttpServletRequest request, HttpSession session, JobTmp jobTmp) throws Exception
    {
        Job job = new Job();
        Object email = session.getAttribute("user");
        int id = companyService.findCompanyIdByEmail(email.toString());

        job.setCompanyDetailId(id);
        job.setCityId(companyService.returnCityId(jobTmp.getWorkAddress()));
        job.setaAddress(jobTmp.getPositionAddress());
        job.setDegreeId(companyService.returnDegreeId(jobTmp.getEducation()));
        job.setExperienceId(companyService.returnWorkyearId(jobTmp.getWorkYear()));
        job.setIsPublished(1);
        job.setJobBeauty(jobTmp.getJobBeauty());
        job.setJobDiscription(jobTmp.getPositionDescribe());
        job.setJobRequire(jobTmp.getPositionRequire());
        job.setsStatus(1);
        job.setJobName(jobTmp.getPositionName());
        job.setJobNumber(jobTmp.getJobNumber());
        job.setSalaryTypeId(companyService.returnSalaryId(jobTmp.getSalary()));
        job.setJobTypeId(companyService.returnJobType2Id(jobTmp.getPositionType()));
        job.setType2_id(companyService.returnJobType2Id(jobTmp.getPositionType()));
        job.setType3_id(companyService.returnJobType3Id(jobTmp.getPositionName()));
        job.setPropertyID(companyService.returnJobPropertyId(jobTmp.getJobNature()));
        Date time = new java.sql.Date(new java.util.Date().getTime());
        job.setDateAdd(time);
        int resultid = companyService.insertJob(job);
        System.out.println("----------------------"+resultid);
        boolean flag = false;
        if(resultid != 0 )
            flag = true;


        return "user/create";
    }

    @RequestMapping(value = "returnJobByStatus", method = RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultJobList returnJobByStatus(HttpSession session, @RequestParam(value = "type", required = true) Integer status) throws Exception
    {

        ResultJobList resultJobList = new ResultJobList();
        Object email = session.getAttribute("user");
        int id = companyService.findCompanyIdByEmail(email.toString());
        List<Job> list = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (status == 1){
            list = companyService.findJob1Status(id);
            for(int i=0;i<list.size();i++){
                Job item = list.get(i);
                item.setCityStr(companyService.returnCityById(item.getCityId()));
                item.setPropertyStr(companyService.returnJobPropertyById(item.getPropertyID()));
                item.setSalaryStr(companyService.returnSalaryById(item.getSalaryTypeId()));
                item.setExperienceStr(companyService.returnWorkyearById(item.getExperienceId()));
                item.setDegreeStr(companyService.returnDegreeById(item.getDegreeId()));
                item.setDateAddStr(df.format(item.getDateAdd()));
            }
        }else if(status == 0){
            list = companyService.findJob0Status(id);
            for(int i=0;i<list.size();i++){
                Job item = list.get(i);
                item.setCityStr(companyService.returnCityById(item.getCityId()));
                item.setPropertyStr(companyService.returnJobPropertyById(item.getPropertyID()));
                item.setSalaryStr(companyService.returnSalaryById(item.getSalaryTypeId()));
                item.setExperienceStr(companyService.returnWorkyearById(item.getExperienceId()));
                item.setDegreeStr(companyService.returnDegreeById(item.getDegreeId()));
                item.setDateAddStr(df.format(item.getDateAdd()));
            }
        }

        if(list != null){
            resultJobList.setSuccess(true);
            resultJobList.setMsg("success");
            resultJobList.setJobs(list);
        }else {
            resultJobList.setSuccess(false);
            resultJobList.setMsg("success");
        }
        return resultJobList;
    }

    @RequestMapping(value = "returnJobById", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody String returnJobById(int jobId) throws Exception
    {

        return "success";
    }

    @RequestMapping(value = "deleteJobById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody Result deleteJobById(@RequestParam(value = "jobId", required = true) Integer jobId) throws Exception
    {
        //System.out.println("----------------           -----------------"+jobId);
        companyService.deleteJobById(jobId);
        Result result = new Result();
        result.setSuccess(true);
        result.setMsg("success");
        return result;
    }

    @RequestMapping(value = "updateJobById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody String updateJobById(Job job) throws Exception
    { 

        return "success";
    }

    @RequestMapping(value = "returnDeliveryByStatus", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultDeliveryActivityList returnDeliveryByStatus(HttpSession session, @RequestParam(value = "type", required = true)Integer status) throws Exception
    {
        ResultDeliveryActivityList resultDeliveryActivityList = new ResultDeliveryActivityList();
        List<DeliveryActivity> list = null;
        Object email = session.getAttribute("user");
        int id = companyService.findCompanyIdByEmail(email.toString());
        if(status == 0){
            list = companyService.findDeliveryBy0State(id);
            list = chulideliverylist(list);
        }else if(status == 1){
            list = companyService.findDeliveryBy1State(id);
            list = chulideliverylist(list);
        }else if(status == 2){
            list = companyService.findDeliveryBy2State(id);
            list = chulideliverylist(list);
        }

        if(list != null){
            resultDeliveryActivityList.setMsg("success");
            resultDeliveryActivityList.setSuccess(true);
            resultDeliveryActivityList.setActivities(list);
        }else {
            resultDeliveryActivityList.setSuccess(false);
            resultDeliveryActivityList.setMsg("success");
        }
        return resultDeliveryActivityList;
    }

    static List<DeliveryActivity> chulideliverylist(List<DeliveryActivity> list){
        for(int i=0;i<list.size();i++){
            DeliveryActivity item = list.get(i);
            if (item.getDelivery().getResume().getCustomer().getsSex() == 0){
                item.getDelivery().getResume().getCustomer().setStrSex("男");
            }else if (item.getDelivery().getResume().getCustomer().getsSex() == 1){
                item.getDelivery().getResume().getCustomer().setStrSex("女");
            }
        }
        return list;
    }

    @RequestMapping(value = "setDeliveryAccess", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody Result setDeliverPass(int id) throws Exception
    {
        companyService.setDeliveryAccess(id);
        Result result = new Result();
        result.setMsg("success");
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "setDeliveryReject", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody Result setDeliverReject(int id) throws Exception
    {
        companyService.setDeliveryRejected(id);
        Result result = new Result();
        result.setMsg("success");
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "deleteDeliveryById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody Result deleteDeliverById(int id) throws Exception
    {
        companyService.deleteDeliveryById(id);
        Result result = new Result();
        result.setMsg("success");
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "manageJobs",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody Result home(int id, int type) throws Exception
    {
        if(type == 1){
            companyService.changeJob1Status(id);
        }else if(type == 0){
            companyService.changeJob0Status(id);
        }
        Result result = new Result();
        result.setMsg("success");
        result.setSuccess(true);
        return result;
    }

    @RequestMapping("returnHome")
    public String returnHome(HttpSession session, Model model)throws Exception{
        String user = (String) session.getAttribute("user");
        if(session.getAttribute("isCom") == true)
        {
            model.addAttribute("user",user);
            return "user/create";
        }else {
            return "home/index";
        }
    }
    @RequestMapping("create")
    public String create(HttpSession session, Model model)throws Exception {
        String user = (String) session.getAttribute("user");
        if(session.getAttribute("isCom") == true)
        {
            model.addAttribute("user",user);
            return "user/create";
        }else {
            return "home/index";
        }
    }

    @RequestMapping("returnToHome")
    public String returnToHome(HttpSession session, Model model)throws Exception{
        String user = (String) session.getAttribute("user");
        model.addAttribute("user",user);
        return "home/index";
    }

}
